﻿using System;
using Game;
using Game.ui;
using GameFramework.events;
using GameFramework.scene;
using GameFramework.timer;
using GameFramework.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameInitingPresenter
{


    public GameInitingPresenter()
    {
        MVPEventManager.Instance.LoadinView_V2P_Show += loadingViewShow;
    }

    public void loadingViewShow()
    {
        TimerManager.Instance.schedule(timer =>
        {
            checkStared();
        }, 2.3f, 1);
    }
    private void checkStared()
    {
        //检查是否完成
        if (AllData.Instance.gameInited)
        {
            //跳到主场景
            //检查是否已经有本地玩家(steam玩家数据)
            BaseInfoData baseInfoData = AllData.Instance.mainPlayerData.baseInfoData;
            bool hadPlayerData =!(baseInfoData==null || baseInfoData.name == null || baseInfoData.name.Equals(""));
            
            showNextView(hadPlayerData);
            SceneLoadingManager.Instance.LoadingSceneSync("Home", () =>
            {
                UIManager.Instance.show<HomeView>();
            });
        }
        else
        {
            //TODO 超过1分钟则提示有问题，直接退出游戏
            //继续等待
            TimerManager.Instance.schedule(timer =>
            {
                checkStared();
            }, 2f, 10);
        }
    }



    private void showNextView(bool hadPlayerData)
    {
        
        if (hadPlayerData)
        {
            UIManager.Instance.show<HomeView>();
            return;
        }
        UIManager.Instance.show<SetNamingView>(false);
    }
}